<?php

class SearchController extends BaseController
{
        public function init() {
            parent::init();
            setLanguage(isset($_GET['lang']) ? $_GET['lang'] : null);
        }
        
	public function actionIndex()
	{
                $keyword=$_GET["keyword"];
                if($keyword=='')
                {
                    $this->render('index',array('dataProvider'=> new CActiveDataProvider('Article',array('data'=>array(),'totalItemCount'=>0))));
                }
                else
                {
                    $dataProvider = Article::model()->searchArticle($keyword,Yii::app()->session['lang']);
                    $this->render('index',array('dataProvider'=>$dataProvider));
                }                
	}
        
        public function shortenString($string, $wordsReturned)
        {
            $retVal = '';
            $isShortened = false;
            $string = html_entity_decode($string);
            $string = strip_tags($string);
            //exclude space
            $array = preg_split('/[\s]+/', trim($string));
            if (count($array) <= $wordsReturned)
            {
                $retVal = $string;
            }
            else
            {
                array_splice($array, $wordsReturned);
                $retVal = implode(" ", $array)." ...";
                $isShortened = true;
            }
            return array('isShortened'=>$isShortened,'retVal'=>$retVal);
        }


	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}